﻿using ProTrack.Entity;
using ProTrack.IRepository;
using ProTrack.IServices.Product;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ProTrack.Services.Product
{
    /// <summary>
    /// 型号业务逻辑层
    /// </summary>
    public class ModelServices : IModelServices
    {
        public ModelServices(IBaseRepository db)
        {
            DB = db;
        }
        private IBaseRepository DB { get;}
        /// <summary>
        /// 新增型号方法
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> AddAsync(Model model)
        {
            var result = await DB.InsertAsync(model);
            return result;
        }
        /// <summary>
        /// 批量删除型号方法
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(int[] Ids)
        {

            string sql = "delete Model where Id in @Ids";
            var result = await DB.DeleteAsync(sql, new { Ids });
            return result;
        }
        /// <summary>
        /// 删除指定型号方法
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(int Id)
        {
            string sql = "delete Model where Id = @Id";
            var result = await DB.DeleteAsync(sql, new { Id });
            return result;
        }
        /// <summary>
        /// 根据页码，页大小，编号和名称获取型号信息方法
        /// </summary>
        /// <param name="PageIndex">页码</param>
        /// <param name="PageSize">页大小</param>
        /// <param name="Number">型号编号</param>
        /// <param name="Name">型号名称</param>
        /// <returns></returns>
        public async Task<IEnumerable<Model>> GetAsync(int PageIndex, int PageSize, string Number, string Name)
        {
            string sql = "select * from Model where 1=1 ";
            if (!string.IsNullOrEmpty(Number))
            {
                sql += " and Number like @Number ";
            }
            if (!string.IsNullOrEmpty(Name))
            {
                sql += " and Name like @Name ";
            }
            sql += " order by CreateTime desc offset @PageSize*(@PageIndex-1) row fetch next @PageSize rows only ";
            var list = await DB.QueryAsync<Model>(sql, new { PageIndex, PageSize, Number = $"%{Number}%", Name = $"%{Name}%" });
            return list;
        }
        /// <summary>
        /// 获取所有型号方法
        /// </summary>
        /// <returns></returns>
        public async Task<IEnumerable<Model>> GetAllAsync()
        {
            string sql = "select * from Model";
            var list = await DB.QueryAllAsync<Model>(sql);
            return list;
        }
        /// <summary>
        /// 更新型号信息方法
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(Model model)
        {
            var result = await DB.UpdateAsync(model);
            return result;
        }
        /// <summary>
        /// 根据编号和名称获取型号数量方法
        /// </summary>
        /// <param name="Number">型号编号</param>
        /// <param name="Name">型号名称</param>
        /// <returns></returns>
        public async Task<int> GetCountAsync(string Number, string Name)
        {
            string sql = "select count(*) from Model where 1=1 ";
            if (!string.IsNullOrEmpty(Number))
            {
                sql += " and Number like @Number ";
            }
            if (!string.IsNullOrEmpty(Name))
            {
                sql += " and Name like @Name ";
            }
            var count = await DB.GetRecoreCountAsync(sql, new { Number = $"%{Number}%", Name = $"%{Name}%" });
            return count;
        }
    }
}
